﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using InnovaApp.Layers.CommonLayer;
using InnovaApp.Layers.DataLayer;
using InnovaApp.Layers.ResourcesLayer.Properties;
using InnovaApp.Layers.SecurityLayer.Models;
using InnovaApp.Layers.SecurityLayer.Models.AccountModels.MembershipServices;
using INNOVACALL.Framework.Common;
using System.Web.Security;
using INNOVACALL.Framework.Common.CodeFormatting;
using INNOVACALL.Framework.Common.Models;

namespace InnovaApp.Layers.SecurityLayer.Controllers
{
	public class ChangePasswordController : _Controllers_Base<ChangePasswordModel>
	{
		public IFormsAuthenticationService FormsService { get; set; }
		public IMembershipService MembershipService { get; set; }

		/// <summary>
		/// Initializes data that might not be available when the constructor is called.
		/// </summary>
		/// <param name="requestContext">The HTTP context and route data.</param>
		protected override void Initialize(RequestContext requestContext)
		{
			base.eMethodBase = "InnovaApp.Layers.SecurityLayer.Controllers.ChangePasswordController";
			base.specificCssCollection = new string[] { "_FormCommon.css", "Views/Account/AccountCommon.css", "ClientComponents/CodeFormatting/csharp.css", "ClientComponents/KendoUI/kendo.common.min.css", "ClientComponents/KendoUI/kendo.blueopal.min.css" };
			base.specificJSCollection = null;
			base.dynamicJSCollection = new string[] { "/ScriptsG/inoChangePasswordDynamic.js" };
			base.specificJqueryJSCollection = new string[] { "ClientComponents/KendoUI/kendo.web.min.js", "InnovacallJS/ChangePassword/ChangePassword.js" };
            base.jsVariables = null;
            base.externalCSS = null;
			base.Meta.Add(enMeta.Description, Resources.View_ChangePassword_Meta_Description);
			base.Meta.Add(enMeta.Keywords, Resources.View_ChangePassword_OrderWithDetails_Meta_Keywords);
			base.Meta.Add(enMeta.Subject, Resources.View_ChangePassword_OrderWithDetails_Meta_Subject);

			if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
			if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

			base.Initialize(requestContext);
		}
		/// <summary>
		/// URL[Get]: /ChangePassword
		/// </summary>
		/// <returns></returns>
		[Authorize, HttpGet]
		public ActionResult Index()
		{
			using (DataRepositories _dataContext = new DataRepositories())
			{
				base.InitView(
					Resources.View_Register_PageTitle,
					string.Empty,
					specificCssCollection,
					specificJSCollection,
					dynamicJSCollection,
					specificJqueryJSCollection,
                    jsVariables,
					externalCSS,
					Meta,
					_dataContext,
					true
					);
			}

			#region Involved code rendering
			INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel InvolvedSourceCode;
			INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel InvolvedSubSourceCode;
			ContentData.InvolvedCode = new InvolvedCodeModel();
			InvolvedSourceCode = new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel();

			#region Views, partials & template editors
			InvolvedSourceCode.TabStripName = "Views, partials & template editors";
			InvolvedSourceCode.TabStripContent = string.Empty;
			InvolvedSourceCode.Language = enCodeLanguage._MultipleCodes;

			InvolvedSubSourceCode = new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.Html, TabStripName = "View", TabStripContent = "/Views/ChangePassword/Index.cshtml" };
			InvolvedSourceCode.SubCodes.Add(InvolvedSubSourceCode);

			InvolvedSubSourceCode = new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.Html, TabStripName = "Layout", TabStripContent = "/Views/Shared/_Layout.cshtml" };
			InvolvedSourceCode.SubCodes.Add(InvolvedSubSourceCode);

			InvolvedSubSourceCode = new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.Html, TabStripName = "Editor Template", TabStripContent = "/Views/Shared/EditorTemplates/ChangePassword_MetaModel.cshtml" };
			InvolvedSourceCode.SubCodes.Add(InvolvedSubSourceCode);

			ContentData.InvolvedCode.Add(InvolvedSourceCode);
			#endregion

			ContentData.InvolvedCode.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.cSharp, TabStripName = "Controller", TabStripContent = "../../Layers/SecurityLayer/Controllers/ChangePasswordController.cs" });

			#region Models
			InvolvedSourceCode = new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel();
			InvolvedSourceCode.TabStripName = "Models, MetaModels & MetaData";
			InvolvedSourceCode.TabStripContent = string.Empty;
			InvolvedSourceCode.Language = enCodeLanguage._MultipleCodes;

			InvolvedSubSourceCode = new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.cSharp, TabStripName = "Main model", TabStripContent = "../../Layers/SecurityLayer/Models/ChangePasswordModel.cs" };
			InvolvedSourceCode.SubCodes.Add(InvolvedSubSourceCode);

			InvolvedSubSourceCode = new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.cSharp, TabStripName = "Main model", TabStripContent = "../../Layers/SecurityLayer/Models/MetaModels/ChangePassword_MetaModel.cs" };
			InvolvedSourceCode.SubCodes.Add(InvolvedSubSourceCode);

			InvolvedSubSourceCode = new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.cSharp, TabStripName = "Main model", TabStripContent = "../../Layers/SecurityLayer/Models/MetaData/ChangePassword_MetaData.cs" };
			InvolvedSourceCode.SubCodes.Add(InvolvedSubSourceCode);
			#endregion

			#region Javascripts
			InvolvedSourceCode = new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel();
			InvolvedSourceCode.TabStripName = "Javascripts";
			InvolvedSourceCode.TabStripContent = string.Empty;
			InvolvedSourceCode.Language = enCodeLanguage._MultipleCodes;
			InvolvedSourceCode.SubCodes.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.Javascript, TabStripName = "View JS", TabStripContent = "/Scripts/InnovacallJS/ChangePassword/ChangePassword.js" });
			InvolvedSourceCode.SubCodes.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.Javascript, TabStripName = "Layout JS", TabStripContent = "/Scripts/InnovacallJS/_Layout/_Layout.js" });
			ContentData.InvolvedCode.Add(InvolvedSourceCode);
			#endregion

			#region CSS
			InvolvedSourceCode = new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel();
			InvolvedSourceCode.TabStripName = "CSS";
			InvolvedSourceCode.TabStripContent = string.Empty;
			InvolvedSourceCode.Language = enCodeLanguage._MultipleCodes;
			InvolvedSourceCode.SubCodes.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.Css, TabStripName = "View CSS", TabStripContent = "/Content/Views/Account/AccountCommon.css" });
			InvolvedSourceCode.SubCodes.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.Css, TabStripName = "Form Common CSS", TabStripContent = "/Content/_FormCommon.css" });
			InvolvedSourceCode.SubCodes.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.Css, TabStripName = "Site (Layout) CSS", TabStripContent = "/Content/_Site.css" });
			ContentData.InvolvedCode.Add(InvolvedSourceCode);
			#endregion

			#region Generated client files
			InvolvedSourceCode = new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel();
			InvolvedSourceCode.TabStripName = "Generated client files";
			InvolvedSourceCode.TabStripContent = string.Empty;
			InvolvedSourceCode.Language = enCodeLanguage._MultipleCodes;

			InvolvedSubSourceCode = new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel();
			InvolvedSubSourceCode.TabStripName = "Javascript";
			InvolvedSubSourceCode.TabStripContent = string.Empty;
			InvolvedSubSourceCode.SubCodes.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.cSharp, TabStripName = "Controller", TabStripContent = "/Controllers/ScriptsGController.cs" });
			InvolvedSubSourceCode.SubCodes.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.Html, TabStripName = "View", TabStripContent = "/Views/ScriptsG/inoChangePasswordDynamic.cshtml" });
			InvolvedSubSourceCode.SubCodes.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.GeneratedJavascript, TabStripName = "Generated javascript", TabStripContent = "/ScriptsG/inoChangePasswordDynamic.js" });
			InvolvedSourceCode.SubCodes.Add(InvolvedSubSourceCode);

			InvolvedSubSourceCode = new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel();
			InvolvedSubSourceCode.TabStripName = "CSS";
			InvolvedSubSourceCode.TabStripContent = string.Empty;
			InvolvedSubSourceCode.SubCodes.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.cSharp, TabStripName = "Controller", TabStripContent = "/Controllers/CSSGController.cs" });
			InvolvedSubSourceCode.SubCodes.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.Html, TabStripName = "View", TabStripContent = "/Views/CSSG/site.cshtml" });
			InvolvedSubSourceCode.SubCodes.Add(new INNOVACALL.Framework.Common.Models.MetaModels.CodeFile_MetaModel() { Language = enCodeLanguage.GeneratedCss, TabStripName = "Generated CSS (from _site.css)", TabStripContent = "/CSSG/Site.css" });
			InvolvedSourceCode.SubCodes.Add(InvolvedSubSourceCode);

			ContentData.InvolvedCode.Add(InvolvedSourceCode);
			#endregion

			RenderCodeFiles(ContentData.InvolvedCode);
			#endregion

			return View(ContentData);
		}

		/// <summary>
		/// URL[Post]: /ChangePassword
		/// </summary>
		/// <returns></returns>
		[Authorize, HttpPost]
		public JsonResult Index(ChangePasswordModel model)
		{
			JsonResult toReturn = new JsonResult { JsonRequestBehavior = JsonRequestBehavior.AllowGet };
			//Disabled for demo
			//MembershipUser user = Membership.GetUser();
			//if (MembershipService.ChangePassword(user.UserName, model.PasswordFields.OldPassword, model.PasswordFields.NewPassword))
			//{
			//    toReturn.Data = new { @success=true , @messageTitle=Resources.View_ChangePassword_Password_Changed_Successfully_MessageTitle, @message = Resources.View_ChangePassword_Password_Changed_Successfully };
			//}
			//else {
			//    toReturn.Data = new { @success = false, @message = Resources.View_ChangePassword_Failed };
			//}
			toReturn.Data = new { @success = false, @message = Resources.View_ChangePassword_PasswordModificationDisabledForDemo };
			return toReturn;
		}


	}
}
